package com.qiuling.iot.utils;

import com.qiuling.iot.Exception.code.FunctionCodeEnum;
import com.qiuling.iot.constants.Constant;
import com.qiuling.iot.entity.GprsData;
import com.qiuling.iot.vo.req.ClientSendReqVO;
import com.qiuling.iot.vo.req.ServerSendReqVO;
import com.qiuling.iot.vo.resp.ServerReceiveRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author：zhangqiang
 * @Description：gprs业务工具类
 * @Date：Created in 21:59 2020/3/4
 * @Modified By：
 */
@Component
public class GPRSUtils {
    //创建线程池
    private ExecutorService cacheExecutor = Executors.newCachedThreadPool();

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 客户端发松心跳码
     * @param url
     * @param equipmentCode
     * @return
     */
    public ClientSendReqVO sendHeratCode(String url, String equipmentCode) {
        GprsData gprs = restTemplate.getForObject(url, GprsData.class);
        ClientSendReqVO vo = new ClientSendReqVO();
        if(gprs != null && gprs.getEquipmentCode().equals(equipmentCode)) {
            cacheExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    byte[] bytes = HexDump.hexStringToByteArray(gprs.getData());
                    //发送心跳码
                    if(bytes.length == 4) {
                        if(bytes[3] == FunctionCodeEnum.HEART.getValue()) {
                            vo.setFunctionCode(FunctionCodeEnum.HEART.getValue());

                        }
                    }
                }
            });

        }
        return vo;
    }

    /**
     * 服务器接收数据
     * @param url
     * @param equipmentCode
     * @return
     */
    public ServerReceiveRespVO receiveData(String url, String equipmentCode) {
        GprsData gprs = restTemplate.getForObject(url, GprsData.class);
        ServerReceiveRespVO vo = new ServerReceiveRespVO();
        if(gprs != null && gprs.getEquipmentCode().equals(equipmentCode)) {
            vo.setEquipmentCode(equipmentCode);
            vo.setMessage(gprs.getData());
            vo.setReceiveTime(new Date());
        }
        return vo;
    }

    /**
     * 服务器发送电机控制码
     * @param url
     * @param equipmentCode
     * @return
     */
    public ServerSendReqVO sendData(String url, String equipmentCode) {
        GprsData gprs = restTemplate.getForObject(url, GprsData.class);
        ServerSendReqVO vo = new ServerSendReqVO();
        vo.setFunctionCode(FunctionCodeEnum.MESSAGE.getValue());
        if(gprs != null && gprs.getEquipmentCode().equals(equipmentCode)) {
            byte[] bytes = HexDump.hexStringToByteArray(gprs.getData());
            //发送控制码
            if(bytes.length >= 6) {
                if(bytes[3] == Constant.GPRS_MOTOR_CONTROL_CODE) {
                    String hexString = HexDump.dumpHexString(bytes, 4, bytes.length);
                    String[] hexArr = hexString.split(" ");
                    for(int i = 0; i < hexArr.length; i++) {
                        if(hexArr[i].substring(1).equals(equipmentCode.substring(0, equipmentCode.length()-1))) {
                            String code = hexArr[i].substring(0,  hexArr[i].length()-1);
                            switch (code) {
                                case "0": vo.setMessage("0");
                                case "1": vo.setMessage("1");
                            }
                        }
                    }
                }
            }
        }
        return vo;
    }
}
